//
//   Copyright (c) 2009 Bálint Jánvári
//       www.programmaticmagic.com
//  
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.

//
//  OMFontPickerController.m
//  ObjectiveMagic
//

#import "OMFontPickerController.h"


@implementation OMFontPickerController
@synthesize delegate;

- (id)init
{
	return [self initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithDelegate:(NSObject<OMFontPickerControllerDelegate>*)d
{
	delegate = d;
	return [self initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithStyle:(UITableViewStyle)style
{
	NSMutableArray* fontList = [[NSMutableArray alloc] init];
	NSArray* familyNames = [UIFont familyNames];
	for (NSString* familyName in familyNames)
	{
		NSArray* fontNames = [UIFont fontNamesForFamilyName:familyName];
		for (NSString* fontName in fontNames)
		{
			[fontList addObject:fontName];
		}
	}
	fonts = [[NSArray alloc] initWithArray:fontList];
	[fontList release];
	return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(goCancel)] autorelease];
	self.title = @"Select font";
}

- (void)goCancel
{
	if ([delegate respondsToSelector:@selector(fontPickerControllerDidCancel:)])
	{
		[delegate fontPickerControllerDidCancel:self];
	}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView 
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [fonts count];
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString* CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
	{
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	cell.textLabel.text = [fonts objectAtIndex:indexPath.row];
	cell.textLabel.font = [UIFont fontWithName:cell.textLabel.text size:cell.textLabel.font.pointSize];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	if ([delegate respondsToSelector:@selector(fontPickerController:didFinishPickingFont:)])
	{
		[delegate fontPickerController:self didFinishPickingFont:[UIFont fontWithName:[fonts objectAtIndex:indexPath.row] size:[UIFont systemFontSize]]];;
	}
}

- (void)dealloc {
	[fonts release];
    [super dealloc];
}


@end

